package com.example.DataSource.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.DataSource.Service.answerService;
import com.example.DataSource.entity.*;
import com.example.DataSource.Mapper.answerMapper;
import com.example.DataSource.entity.answer_replies;
import com.example.DataSource.entity.answer_replies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class answerServiceImpl extends ServiceImpl<answerMapper, answer_replies> implements answerService {

    @Override
    @Transactional
    public IPage<answer_replies> getAnswerPage(Page<answer_replies> page) {
        QueryWrapper<answer_replies> queryWrapper =  new QueryWrapper<>();
        // 使用 MyBatis-Plus 的分页查询方法，查询未删除的记录
        IPage<answer_replies> answerPage = this.page(page,queryWrapper);
        // 获取查询结果的记录列表
        List<answer_replies> records = answerPage.getRecords();
        // 遍历记录列表，为每个记录设置 consultantName
        for (answer_replies ask : records) {
            // 调用 getConsultantName 方法获取 member_name
            String consultantName = getConsultantName(ask.getConsultant_id());
            // 将获取到的 member_name 设置到记录中
            ask.setConsultant_name(consultantName);
        }
//        遍历记录列表，为每个记录设置问题内容
        for (answer_replies ask : records) {
//            调用 getQuestion 方法获取问题内容
            ask.setQuestion(getQuestion(ask.getConsultant_id()));
        }
        // 返回包含 member_name 的分页结果
        return answerPage;
    }

    @Autowired
    private consultantServiceImpl consultantService;
    private String getConsultantName(int consultantId) {
//        System.out.println(consultantId);
        QueryWrapper<consultant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",consultantId);
        List<consultant> list = consultantService.list(queryWrapper);
//        System.out.println(list.toString());
        return list.get(0).getName();
    }

    @Autowired
    private psychoServiceImpl psychoService;
    private String getQuestion(int questionId){
        QueryWrapper<psycho_ask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",questionId);
        return psychoService.list().get(0).getTitle();
    }

    @Override
    @Transactional
    public IPage<answer_replies> getAnswerPageSearch(Page<answer_replies> page, String searchInfo) {
        QueryWrapper<answer_replies> queryWrapper =  new QueryWrapper<>();
        queryWrapper
                .like("content",searchInfo);
        // 使用 MyBatis-Plus 的分页查询方法，查询未删除的记录
        IPage<answer_replies> answerPage = this.page(page,queryWrapper);
        // 获取查询结果的记录列表
        List<answer_replies> records = answerPage.getRecords();
        // 遍历记录列表，为每个记录设置 consultantName
        for (answer_replies ask : records) {
            // 调用 getConsultantName 方法获取 member_name
            String consultantName = getConsultantName(ask.getConsultant_id());
            // 将获取到的 member_name 设置到记录中
            ask.setConsultant_name(consultantName);
        }
//        遍历记录列表，为每个记录设置问题内容
        for (answer_replies ask : records) {
//            调用 getQuestion 方法获取问题内容
            ask.setQuestion(getQuestion(ask.getConsultant_id()));
        }
        // 返回包含 member_name 的分页结果
        return answerPage;
    }
}
